package com.tcu.mosaicdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by chenlong on 15/8/20.
 */
public class MosaicView extends ViewGroup{
    private static final String TAG = MosaicView.class.getSimpleName();
    public  static final float WH_SCALE = 9/16;
    private List<String> mPaths;
    private Context mContext;
    private int mPicHeigth = 0;//生成的图片的高度
    private int mPicWidth = 0;//生成的图片的宽度
    private MosaicModel mMosaicModel;
    private double leftblock_width;
    private double leftblock_height;
    private double rightblock_width;
    private double rightblock_height;
    private double bottomblock_width;
    private double bottomblock_height;
    private int width ;
    private int height;
    public MosaicView(Context context) {
        super(context);
        init(context);
    }

    public MosaicView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MosaicView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    private void init(Context context){
        mContext = context;
    }
    private void createBitmap(){
        mPicHeigth = getHeight();
        mPicWidth = (int)(mPicHeigth*WH_SCALE);
        Log.d(TAG, "height = " + mPicHeigth + "width = " + mPicWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d(TAG, "onDraw");
        createBitmap();


    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        //根据指定的拼图方式计算单块拼图的大小
        leftblock_width = width*0.6;
        leftblock_height = height*0.8;
        rightblock_width = width*0.7;
        rightblock_height = height*0.5;
        bottomblock_width = width;
        bottomblock_height = height*0.5;
        if(getChildCount()==3){
            measureChild(getChildAt(2),
                    MeasureSpec.makeMeasureSpec((int) leftblock_width, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec((int) leftblock_height, MeasureSpec.EXACTLY)
            );
            measureChild(getChildAt(0),
                    MeasureSpec.makeMeasureSpec((int) rightblock_width, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec((int) rightblock_height, MeasureSpec.EXACTLY)
            );
            measureChild(getChildAt(1),
                    MeasureSpec.makeMeasureSpec((int) bottomblock_width, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec((int) bottomblock_height, MeasureSpec.EXACTLY)
            );

        }
        setMeasuredDimension(width, height);
    }
    @Override
    protected void onLayout(boolean b, int left, int top, int right, int bottom) {
        if (getChildCount() == 3) {


            int rightchild_left = (int) (leftblock_width - width * 0.3);

            MosaicBlockView rightchild = (MosaicBlockView)getChildAt(0);
            rightchild.layout(left + rightchild_left, 0, left + rightchild_left + rightchild.getMeasuredWidth(), rightchild.getMeasuredHeight());
            Path rightPath = new Path();
            rightPath.moveTo(0, 0);
            rightPath.lineTo(0, (float) rightblock_height);
            rightPath.lineTo((float) (rightblock_width * 0.4285), (float) rightblock_height);
            rightPath.close();
            rightchild.setDrawPath(rightPath);
            MosaicBlockView bottomchild = (MosaicBlockView)getChildAt(1);
            bottomchild.layout(left, (int) (height * 0.5), left + bottomchild.getMeasuredWidth(), (int) (height * 0.5) + bottomchild.getMeasuredHeight());
            Path bottomPath = new Path();
            bottomPath.moveTo(0,0);
            bottomPath.lineTo(0,(float)(0.6*bottomblock_height));
            bottomPath.lineTo((float)(0.6*bottomblock_width),0);
            bottomPath.close();
            bottomchild.setDrawPath(bottomPath);
            MosaicBlockView leftchild = (MosaicBlockView)getChildAt(2);

            leftchild.layout(left, 0, left + leftchild.getMeasuredWidth(), leftchild.getMeasuredHeight());
            Path path = new Path();
            path.moveTo((float)(leftblock_width*0.5),0);
            path.lineTo((float) leftblock_width,(float) (leftblock_height * 0.625));
            path.lineTo(0, (float) leftblock_height);
            path.lineTo((float) leftblock_width, (float) leftblock_height);
            path.lineTo((float) leftblock_width, 0);
            path.close();
            leftchild.setDrawPath(path);
            leftchild.invalidate();
        }
    }





    /***Public Method ***/

    /**
     * 设置图片路径
     * @param pathlist
     */
    public void setPathList(List<String> pathlist) {
        Log.d(TAG, "setPath");
        mPaths = pathlist;
        //添加三个拼图块
        MosaicBlockView rightView = new MosaicBlockView(mContext);
        rightView.setPath(pathlist.get(1));
        rightView.setBackgroundColor(Color.BLUE);
        addView(rightView);

        MosaicBlockView bottomView = new MosaicBlockView(mContext);
        bottomView.setPath(pathlist.get(2));
        bottomView.setBackgroundColor(Color.GREEN);
        addView(bottomView);

        MosaicBlockView leftView = new MosaicBlockView(mContext);
        leftView.setPath(pathlist.get(0));
        addView(leftView);
        invalidate();
    }
    /**
     * no use
     */
    public void setMosaicModel(MosaicModel mosaicModel,List<String> pathList){
        mMosaicModel = mosaicModel;
        mPaths = pathList;

    }
}
